﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DNASequencing.core.FileParsing;
using DNASequencing.core.data.simple;
using DNASequencing.core.data.complex;
using DNASequencing.core.data.backwardComplex;
using DNASequencing.core.data;
using DNASequencing.core.Loggers;
using DNASequencing.core.Algorithms;
using DNASequencing.core.Algorithms.SeqNeg;
using DNASequencing.core.Algorithms.SeqNegPos;
using DNASequencing.core.Algorithms.rating;
using System.Diagnostics;

namespace DNASequencing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            algorithmType.SelectedIndex = 0;
        }

        private FileReader fr;
        private SimpleDT fdt;
        private ComplexDT cdt;
        private BackwardComplexDT bdt;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "dna sequences files (*.dna)|*.dna|All files (*.*)|*.*";
            dialog.InitialDirectory = Directory.GetCurrentDirectory();
            dialog.Title = "Select file";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                fr = new FileReader(dialog.FileName);
                fdt = new SimpleDT(fr.sequences);
                cdt = new ComplexDT(fr.sequences);
                bdt = new BackwardComplexDT(fr.sequences);
            }
        }

        private void findButton_Click(object sender, EventArgs e)
        {
            listBox.Text = "";

            List<AOligonucleotide> list = fdt[patternBox.Text];
            if (list == null) return;
            foreach (AOligonucleotide o in list)
            {
                listBox.Text += o.originalSequence + "\n";
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            listBox.Text = "";

            List<AOligonucleotide> list = bdt[patternBox2.Text];
            if (list == null) return;
            foreach (AOligonucleotide o in list)
            {
                listBox.Text += o.originalSequence + "\n";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int k = System.Convert.ToInt32(kBox.Text);
            int n = System.Convert.ToInt32(nBox.Text);

            Stopwatch stopWatch = new Stopwatch();
            AlgorithmResult ar;
            string elapsedTime;
            switch (algorithmType.SelectedIndex)
            {
                case 0: // neg/pos
                    stopWatch.Start();
                    Algorithm alg = new Algorithm(k, n, cdt, new GaussRating(cdt.all.Count));
                    ar = alg.process();
                    stopWatch.Stop();
                    TimeSpan ts = stopWatch.Elapsed;
                    elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
                    listBox.Text += ar + "\n";
                    listBox.Text += "DNA thread length: " + ar.ToString().Length + "\n";
                    listBox.Text += "Positive error: " + alg.positiveError + "\n";
                    listBox.Text += "Negative error: " + alg.negativeError + "\n";
                    listBox.Text += "Run time: " + elapsedTime + "\n";
                    break;
                case 1: // neg
                    stopWatch.Start();
                    NegAlgorithm nalg = new NegAlgorithm(k, n, cdt, new GaussRating(cdt.all.Count));
                    ar = nalg.process();
                    stopWatch.Stop();
                    ts = stopWatch.Elapsed;
                    elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                        ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);
                    listBox.Text += ar + "\n";
                    listBox.Text += "DNA thread length: " + ar.ToString().Length + "\n";
                    listBox.Text += "Positive error: " + ar.positiveError + "\n";
                    listBox.Text += "Negative error: " + ar.negativeError + "\n";
                    listBox.Text += "Run time: " + elapsedTime + "\n";
                    break;
                case 2: // pos
                    break;
            }
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            listBox.Clear();
        }
    }
}
